/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sasp_messages;

import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import sasp_protocol.Entity;

/**
 *
 * @author mauriello
 */
public class PRONodeCommand extends SASPCommand {
    HashSet<Entity> data;

    public PRONodeCommand( ) {
        super( );
        this.setType(PRO_NODE);
    }

    public HashSet<Entity> getData( ) {
        return data;
    }

    @Override
    public void updateCommandBody( ) {
        try {
            JSONObject body = new JSONObject();
            JSONArray array = new JSONArray();
            for(int i = 0; i < data.size(); i++) {
                if( this.getDestinationID() != ((Entity)data.toArray()[i]).GetEntityID()) {
                    JSONObject test = new JSONObject();
                    test.put("Alias", ((Entity)data.toArray()[i]).GetAlias());
                    test.put("ClientID", ((Entity)data.toArray()[i]).GetEntityID());
                    test.put("IPAddress", ((Entity)data.toArray()[i]).getIpString());
                    array.put(test);
                }
            }
            body.put("ClientList", array);
            this.setCommandBody(body);
        } catch (JSONException ex) {
            Logger.getLogger(REQTableReply.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public PRONodeCommand( int _o, int _d, int _m, HashSet<Entity> _tbl ) {
        super( _o, _d, _m );
        this.setType(PRO_NODE);
        saspmanager.SASPManagerApp.addLine("PNC - Promote Node Command Generated");
        data = _tbl;

        try {
            JSONObject body = new JSONObject();
            JSONArray array = new JSONArray();
            for(int i = 0; i < data.size(); i++) {
                if( _d != ((Entity)data.toArray()[i]).GetEntityID()) {
                    JSONObject test = new JSONObject();
                    test.put("Alias", ((Entity)data.toArray()[i]).GetAlias());
                    test.put("ClientID", ((Entity)data.toArray()[i]).GetEntityID());
                    test.put("IPAddress", ((Entity)data.toArray()[i]).getIpString());
                    array.put(test);
                }
            }
            body.put("ClientList", array);
            this.setCommandBody(body);
        } catch (JSONException ex) {
            Logger.getLogger(REQTableReply.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public PRONodeCommand( String _s ) {
        super( _s );

       data = new HashSet<Entity>();
        try {
            saspmanager.SASPManagerApp.addLine("PNC - Promote Node Command Received");
            JSONArray array = this.getCommandBody().getJSONArray("ClientList");
            for( int i = 0; i < array.length(); i++ ) {
                Entity newEnt = new Entity();
                newEnt.SetAlias(((JSONObject)array.get(i)).getString("Alias"));
                newEnt.SetEntityID(((JSONObject)array.get(i)).getInt("ClientID"));
                newEnt.setIpString(((JSONObject)array.get(i)).getString("IPAddress"));
                data.add(newEnt);
            }
        } catch (JSONException ex) {
            Logger.getLogger(REQMasterReply.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    @Override
    public SASPReply execute() {
        saspmanager.SASPManagerApp.addLine("PNC - ACK Reply Generated");
        return null; //new ACKReply(this.getDestinationID(),this.getOriginatorID(),this.getMessageID());
    }
}
